import React, {PureComponent} from 'react'
import {connect} from 'react-redux'
import {Toast} from "antd-mobile"

import Header from "../components/header";
import BetCenterHistory from "../components/betCenterHistory"
import BetCenterLeftTab from "../components/betCenterLeftTab"
import BetCenterRight from "../components/BetCenterRight"

import {loadPlayGameList} from "../redux/actions/indexAction"
import {loadPankKouList,loadPeilvData,loadServenawardDetail,getGameInfo,tempSaveSelectedPeiyu,getLmOrder,tempSaveSelectedNameAndMoney,getMultiGroupOrder,getGroupOrder,autoSelectLM,LMReset} from "../redux/actions/betcenterAction"
import {unserializeContent,serializeContent,orderNum,dingweiTotal} from "../utils/tools"
import {buildQuery} from "../utils/tools"
class BetCenterContainer extends PureComponent {
  constructor (props){
   super(props)
   this.state= {
     num:0,
     money:"",
     totalmoney:0,
     itemcode:"",
     righttabindex:0,
     select:{},
     fenpanSecond:0,
     kaijiangSecond:0,
     fenpanZhong: false,
     kaijiangZhong: false,
     defaultradio:0
   }
   this.initDataTimeout = null
}
    tabsChoose=(key)=>{
      console.log(key);
      this.props.history.replace("/betcenter/"+key)
    }
    getstatedate(){
      const {betcenter,match,dispatch} = this.props
      const gameCode = match.params.key
      const betcenteritem = betcenter.get("pankoulist")[gameCode]
      const peilvitem = betcenter.get("peilv")[gameCode]
      return {gameCode,betcenteritem,peilvitem,dispatch}
    }
    leftTab=(code,index)=>{
      console.log(code);
      const {gameCode,betcenteritem,dispatch} = this.getstatedate()
      if(gameCode == "BJKL8"){
        var righttabcontent =  {}
        // var code = "TM"
      }else{
        var righttabcontent =  betcenteritem.itemList[index]
      }

      this.setState({
        righttabcontent,
        itemcode:code,
        num:0,
        money:"",
        righttabindex:0,
        totalmoney:0,
        select:[]
      })
      if(gameCode=="BJKL8"){
        console.log("fff");
        return  dispatch(loadPeilvData(gameCode,"TM"))
      }
      // console.log("mei");
      
      dispatch(loadPeilvData(gameCode,code))
      // dispatch(getGameInfo(gameCode,code))// 获取开盘信息
      // dispatch(getGameInfo(gameCode,code)) // 获取赔率
      // console.log(this.state.content);
    }
    righttab=(code,index)=>{
      const {gameCode,peilvitem} = this.getstatedate()
      this.setState({
        righttabindex:index,
        num:0,
        money:"",
        totalmoney:0,
        select:[]
      })
      // console.log(code,"righttab");
    }
    digui(n){
      if (n<=1){
        return 1
      }else{
        return n*this.digui(n-1)
      }
    }
    setNum(n,m){
      if(n<m){
        return 0
      }
      return this.digui(n) / (this.digui(n - m) * this.digui(m))
    }
    order(item,key){
        let { select,itemcode,righttabindex} = this.state
        let {match} = this.props;
        if (match.params.key == 'HK6' && itemcode == 'LM') {
          // 香港六合彩 - 连码
          let [leibie, playMode, selectNum, dm1, dm2] = arguments; // 箭头函数没有arguments
          select[0] = {
            leibie,
            playMode,
            selectNum,
            dm1,
            dm2
          }
          // console.log(arguments,"order");
          if(playMode == 1){
            var length = leibie <= 1 ?( selectNum.length>= 3 ? this.setNum(selectNum.length,3) : 0) : (selectNum.length>= 2 ? this.setNum(selectNum.length,2) : 0 )
          }else{
            var length = leibie <= 1 ? ( selectNum.length>= 3 ?  selectNum.length-2 : 0) : ( selectNum.length>= 2 ?  selectNum.length-1 : 0)
          }
        }else{
          // 其他彩种
            let id = item.id
            if(key){
              //单选的
              select[key] = item
            }else{
              select[id] ? delete select[id] : select[id] = item
            }
            var minlen =  0
           
            if(itemcode == "HX"){
              if (righttabindex==0) {
                minlen = 2;
              } else if (righttabindex==1) {
                minlen = 4;
              } else if (righttabindex==2) {
                minlen = 6;
              }
              
            }
            if(itemcode == "ZYBZ"){
              if (righttabindex == 0) {
                minlen = 5;
              } else if (righttabindex == 1){
                minlen = 7
              } else if (righttabindex == 2){
                minlen = 9
              } else if (righttabindex == 3){
                minlen = 4
              } else if (righttabindex == 4){
                minlen = 6;
              }
            }
            if(itemcode == "LX" || itemcode == "LM"){
              if (righttabindex==0) {
                minlen = 2;
              } else if (righttabindex == 1) {
                minlen = 3;
              } else if (righttabindex == 2) {
                minlen = 4;
              } else if (righttabindex == 3) {
                minlen = 5;
              }
              
            }
            if(itemcode == "WSL"){
              if (righttabindex==0) {
                minlen = 2;
              } else if (righttabindex==1) {
                minlen = 3;
              } else if (righttabindex==2) {
                minlen = 4;
              }
            }

            if(itemcode == "ZXS" || itemcode == "ZXL"){
              minlen = 4
              if(itemcode == "ZXS"){
                minlen = 5
              }
                length = Object.keys(select).length >= minlen  ? 1 : 0
            }else if(itemcode == 'DW' || itemcode == 'EZDW' || itemcode == 'SZDW' || itemcode == 'EZZH'){
                var length =  dingweiTotal(select[id].number);
            }else{
              var length =  minlen ===0 ?
              Object.keys(select).length :
              this.setNum(Object.keys(select).length,minlen)
            }
        }
        this.setState({
        select,
        num:length,
        totalmoney:length*this.state.money
        })

        // console.log(select);
    }
    inputmoney(value){
      if(isNaN(value)) {return}
      if(value%1 !==0){
        return Toast.fail("下注金额必须为整数",1)
      }
      this.setState({
        money:value,
        totalmoney:value*this.state.num
      })
    }
    loadCaiPiaoData(props){
      const {dispatch,match,award} = props
      let gameCode = match.params.key
      // let itemCode = this.state.itemcode
      // let awarddetail = award.get("awarddetail")[gameCode]
      dispatch(loadServenawardDetail(gameCode,1)) //获取历史记录
      dispatch(loadPankKouList(gameCode)) //获取盘口
      // dispatch(getGameInfo(gameCode,itemCode)) // 获取开盘信息
      // dispatch(loadPeilvData(gameCode,itemCode)) // 获取赔率
    }
    componentDidMount(){
      const {Index,dispatch} = this.props
      Index.get("playlist").length==0 ? dispatch(loadPlayGameList()) :  Index.get("playlist").map((item,index)=>{
        if(item.gameCode ===  this.props.match.params.key ){
          return  this.setState({
            defaultradio:index
          })
        }
      })
      this.loadCaiPiaoData(this.props)

    }
    componentDidUpdate(prevProps, prevState) {
    }
    setTime(nextProps){
      const gameinfo = nextProps.betcenter.get("gameinfo")
      if (gameinfo) {
        let fenpanSecond = gameinfo.closetime - gameinfo.nowtime;
        let kaijiangSecond = gameinfo.opentime - gameinfo.nowtime;
  
        this.setState({
          fenpanSecond,
          kaijiangSecond
        });
  
        // 封盘结束
        if (fenpanSecond > 0) {
          this.setState({
            fenpanZhong: false,
            kaijiangZhong: false
          });
        }
          
        // 开奖结束
        if (fenpanSecond > 0 && kaijiangSecond > 0 ) {
          this.setState({
            kaijiangZhong: false
          });
        }
  
        // 封盘中
        if (fenpanSecond < 0 && kaijiangSecond > 0) {
          this.setState({
            kaijiangZhong: false,
            fenpanZhong: true,
          });
        }
      
        // 开奖中
        if (fenpanSecond < 0 && kaijiangSecond < 0) {
          this.setState({
            kaijiangZhong: true,
            fenpanZhong: false,
          });
        }
  
        if ( gameinfo.closetime 
          && gameinfo.nowtime 
          && kaijiangSecond < 0 ) {
  
          this.setState({
            kaijiangZhong: true
          });
          
          let _this = this;
          if (this.initDataTimeout) {
            clearTimeout(this.initDataTimeout);
          }
          this.initDataTimeout = setTimeout( () => {
            _this.initDataTimeout = null;
            _this.loadCaiPiaoData();
          },  Math.abs(kaijiangSecond*1000) );
        } else {
          //
        }
      }
      
    }
    shouldComponentUpdate(nextProps, nextState) {
      if (this.props === nextProps && this.state === nextState ) {
        return false;
      } else {
        const {betcenter,match} = this.props
        const gameCode = match.params.key
        const betcenteritem = betcenter.get("pankoulist")[gameCode]
        const peilvitem = betcenter.get("peilv")[gameCode]
        const gameinfo=betcenter.get("gameinfo")
        if(!betcenteritem || !peilvitem) {
         //  console.log(betcenteritem,peilvitem,"de");
          return false
        }
        return true;
      }
    }
  
    componentWillReceiveProps(nextProps){
      var defaultradio = 0
      const {Index} = nextProps

      if(nextProps.Index != this.props.Index){
        Index.get("playlist") && Index.get("playlist").map((item,index)=>{
          if(item.gameCode ===  this.props.match.params.key ){
            return  defaultradio = index
          }
        })
          this.setState({
            defaultradio
          })
      }
      // URL 发生变化后重新加载数据
      if(nextProps.match.params.key != this.props.match.params.key){
        Index.get("playlist") && Index.get("playlist").map((item,index)=>{
          if(item.gameCode ===  nextProps.match.params.key ){
            return  defaultradio = index
          }
        })
              
        this.state= {
          num:0,
          money:"",
          totalmoney:0,
          itemcode:"",
          righttabindex:0,
          select:{},
          fenpanSecond:0,
          kaijiangSecond:0,
          fenpanZhong: false,
          kaijiangZhong: false,
          defaultradio
        }
        this.initDataTimeout = null
        this.loadCaiPiaoData(nextProps)
      }
      this.setTime(nextProps)
    }
    onFenpanFinish () {
      let kaijiangSecond = this.state.kaijiangSecond - this.state.fenpanSecond;
      this.setState({
        fenpanZhong: true,
        fenpanSecond: 0,
        kaijiangSecond: kaijiangSecond
      });
    }
    onKaijiangFinish() {
      console.log('开奖完成...')
      const {itemcode} = this.state
      if(itemcode){
        const {dispatch,match} = this.props
        let gameCode = match.params.key
        let itemCode = itemcode ==="TMSB" ? "TM" : itemcode
        
        dispatch(getGameInfo(gameCode,itemCode)) // 获取开盘信息
        dispatch(loadPeilvData(gameCode,itemCode)) // 获取赔率
      }else{
      this.loadCaiPiaoData(this.props)
     }
      this.setState({
        fenpanZhong: false,
        kaijiangZhong: true,
        fenpanSecond: 0,
        kaijiangSecond: 0
      });
    }
    reset = ()=>{
      this.setState({
        num:0,
        money:"",
        totalmoney:0,
        select:{}
      })
      const {itemcode} = this.state
      const {dispatch,match} = this.props
      if(itemcode == "LM" && match.params.key== "HK6"){
         dispatch(LMReset(true))
      }
    }
    autoclick(body,righttabcontent,itemcode,righttabindex){
      //思路:把内容和 玩法区别开  因为他内容有的返回的不是order之后的数据  先不考虑HK6
      const gameCode = this.props.match.params.key
      // 先拿到右边赔率中的内容
      if(righttabcontent.xzlxList && itemcode != "BB"){
        // var length = righttabcontent.xzlxList.length
        // var page =  body.length / length
        // var bodycontent = body.slice(righttabindex*page,(righttabindex+1)*page)
        var bodycontent = []
            body.map((item,index)=>{
                if(item.id.indexOf(righttabcontent.xzlxList[righttabindex].xzlxCode)>-1){
                  bodycontent.push(item)
                }
            })
      }else if(itemcode == "ZM1T6"){ // 这个完全是没用的 用来测试tab方法的另外一种随机的方式
        var page =  body.length / 6
        var temparr = []
        for(var i =0;i<=6;i++){
          temparr.push(body.slice(i*page,(i+1)*page))
        }
        var randomZM =  parseInt(Math.random()*(6),10) // 总共五个tab 
        var randomnum = parseInt(Math.random()*(temparr[randomZM].length),10) // 对应tab的item数字范围
          console.log(randomZM,randomnum);
         return  this.order(temparr[randomZM][randomnum])
      }else if(itemcode == "LM"){
        if(gameCode=="GDKLSF"){
          var page =  body.length / 6
          var bodycontent = body.slice(righttabindex*page,(righttabindex+1)*page)
        } // 快乐十分连码
        else if(gameCode=="HK6"){ 
          // 定义一个dispatch 方法 通知action type 是真  lm中判别是不是真 就执行 
          const {dispatch} = this.props
          this.setState({
            select:{}
          },()=>{
          dispatch(autoSelectLM(true))
          })
          return
        }
      }else if(itemcode == "GG"){
          let minlen = 2;
          let keys = ["ZM1","ZM2","ZM3","ZM4","ZM5"]
          this.setState({
            select:{}
          }, () =>
          orderNum(minlen,5,(i)=>{
            var bodycontent = body.slice(i*7,(i+1)*7)
            var randomkeynum =  parseInt(Math.random()*(7),10)
            this.order(bodycontent[randomkeynum],keys[i])
          }))
          return
      }else if(['EZDW', 'SZDW', 'EZZH'].indexOf(itemcode) != -1){
          // 这个内容和 玩法 都不同 
        // number: "万:0|千:0"
          // 玩法
           let minlen = 2;
           if (itemcode == 'SZDW') {
             minlen = 3;
           }
          //key
            let keys = ["万","千","百","十","个"]
            let numbers = {}
            function orderNum(){
              var temp = []; // 防止随机出来的数字是一样的
              for(let i=0; i<minlen;i++){
                var randomNum =  parseInt(Math.random()*(5),10)
                tempfuc(temp,randomNum)
                function tempfuc(temp,randomNum){
                  if(temp.indexOf(randomNum) > -1){
                    var randomNum2 =  parseInt(Math.random()*(5),10)
                    tempfuc(temp,randomNum2)
                  }else{
                    temp.push(randomNum)
                  }
                }
                // 内容 都是1-9
                var randomkeynum =  parseInt(Math.random()*(10),10)
                // console.log(temp[i],randomkeynum,"随机的数");
                numbers[keys[temp[i]]] = [randomkeynum]
               } 
              //  console.log(numbers);
            }
              
        this.setState({
          select:{}
        },()=> {
          orderNum()
          var number = serializeContent(numbers);
          let clonecontent = Object.assign({},body[0],{number})
          this.order(clonecontent,clonecontent.id)
        })
          return
      }else if(itemcode == "TMSB"){
        return Toast.fail("该盘口请自己选择",2)
      }else{
        var bodycontent = body
      }
      // console.log(bodycontent,"bbbbbb");
    // 随机最大的数值
      var maxlength = bodycontent.length
    // 定义最小需要选择的个数 
      let minlen = 1
    // 根据玩法 选择需要随机的个数

      if(itemcode == "ZXS"){
        minlen = 5;
      }
      if(itemcode == "ZXL"){
        minlen = 4;
      }
      if(itemcode == "HX"){
        if (righttabindex==0) {
          minlen = 2;
        } else if (righttabindex==1) {
          minlen = 4;
        } else if (righttabindex==2) {
          minlen = 6;
        }
        
      }
      if(itemcode == "ZYBZ"){
        if (righttabindex == 0) {
          minlen = 5;
        } else if (righttabindex == 1){
          minlen = 7
        } else if (righttabindex == 2){
          minlen = 9
        } else if (righttabindex == 3){
          minlen = 4
        } else if (righttabindex == 4){
          minlen = 6;
        }
        // debugger
        // console.log(bodycontent);
        
      }
      if(itemcode == "LX" || itemcode == "LM"){
        if (righttabindex==0) {
          minlen = 2;
        } else if (righttabindex == 1) {
          minlen = 3;
        } else if (righttabindex == 2) {
          minlen = 4;
        } else if (righttabindex == 3) {
          minlen = 5;
        }
        
      }
      if(itemcode == "WSL"){
        if (righttabindex==0) {
          minlen = 2;
        } else if (righttabindex==1) {
          minlen = 3;
        } else if (righttabindex==2) {
          minlen = 4;
        }
      }
      this.setState({
          select:{}
        },()=> orderNum(minlen,maxlength,(i)=>{
          this.order(bodycontent[i])
        }))
        return
    }
    // orderNum(minlen,maxlength,cb=()=>{}){
    //   var temp = []; // 防止随机出来的数字是一样的
    //   for(let i=0; i<minlen;i++){
    //     var randomNum =  parseInt(Math.random()*(maxlength),10)
    //     tempfuc(temp,randomNum)
    //     function tempfuc(temp,randomNum){
    //       if(temp.indexOf(randomNum) > -1){
    //         var randomNum2 =  parseInt(Math.random()*(maxlength),10)
    //         tempfuc(temp,randomNum2)
    //       }else{
    //         temp.push(randomNum)
    //       }
    //     }
    //     cb(temp[i])
    //    }
    //    return temp
    // }

    onBetSubmit() {
      const {match, history, dispatch, betcenter} = this.props;
      const gameCode = match.params.key
      // let pankouItems = betcenter.get('pankoulist');
      const {itemcode,select,money,righttabindex,righttabcontent} = this.state
      const name = betcenter.get("pankoulist")[gameCode].gameName // 获取名字
      // 给与第一个盘口为默认盘口
      let PLitemcode =  itemcode || betcenter.get("defaultitemcode")[gameCode]
      let _this = this;
      if (gameCode == 'HK6' && PLitemcode == 'LM') {
        if (Object.keys(select).length <= 0) {
          Toast.fail('请选择号码');
          return ;
        }
        // 香港六合彩 - 连码  
        let selectedPeiyu = select[0];
        console.log(selectedPeiyu);
        //gameCode, nums, rrtype, pabc, cb = () => {},  dm1 = 0, dm2 = 0
        let rrtype = selectedPeiyu['leibie'];
        let nums = selectedPeiyu['selectNum'];
        
        if ( ( rrtype == 0 || rrtype == 1 ) && nums.length < 3) {
          return Toast.fail('至少选择三个号码');
        } else if ( nums.length < 2 ) {
          return Toast.fail('至少选择二个号码');
        }
  
        dispatch(getLmOrder(gameCode, selectedPeiyu['selectNum'], selectedPeiyu['leibie'], selectedPeiyu['playMode'], (data) => {
            dispatch(tempSaveSelectedPeiyu(select))
            dispatch(tempSaveSelectedNameAndMoney(name,money))
              history.push('/ordermoney?'+buildQuery({
                gameCode: gameCode,
                pankou:PLitemcode,
              }))
            }, selectedPeiyu['dm1'], selectedPeiyu['dm2']))

      }
      else if (gameCode == 'HK6' && PLitemcode == 'ZYBZ') {
  
        let minlen = 0;
        if (righttabindex == 0) {
          minlen = 5;
        } else if (righttabindex == 1){
          minlen = 7
        } else if (righttabindex == 2){
          minlen = 9
        } else if (righttabindex == 3){
          minlen = 4
        } else if (righttabindex == 4){
          minlen = 6;
        }
  
        if (Object.keys(select).length < minlen) {
          Toast.fail(`请至少选择${minlen}个号码`);
          return ;
        }
        
        let nums = [];
        for (let peiyuItemId in select) {
          nums.push(peiyuItemId);
        }
        dispatch(getMultiGroupOrder(gameCode, nums, minlen, (ok,data) => {
          if (ok) {
            dispatch(tempSaveSelectedPeiyu(select));
            dispatch(tempSaveSelectedNameAndMoney(name,money))
            history.push('/ordermoney?'+  buildQuery({
              gameCode: gameCode,
              pankou: PLitemcode,
              subpankou:righttabcontent.xzlxList[righttabindex].xzlxCode
            }));
          } else {
            Toast.fail(data.msg);
          }
        }));
      }
      else if (gameCode == 'HK6' && PLitemcode == 'LX') {
        let minlen = 0;
        if (righttabindex==0) {
          minlen = 2;
        } else if (righttabindex == 1) {
          minlen = 3;
        } else if (righttabindex == 2) {
          minlen = 4;
        } else if (righttabindex == 3) {
          minlen = 5;
        }
      
        if (Object.keys(select).length < minlen) {
          Toast.fail(`请至少选择${minlen}个生肖`);
          return ;
        }
  
        let nums = [];
        for (let peiyuItemId in select) {
          nums.push(peiyuItemId);
        }
  
        dispatch(getMultiGroupOrder(gameCode, nums, minlen, (ok,data) => {
  
          if (ok) {
            dispatch(tempSaveSelectedPeiyu(select));
            dispatch(tempSaveSelectedNameAndMoney(name,money))
            history.push('/ordermoney?'+  buildQuery({
              gameCode: gameCode,
              pankou: PLitemcode,
              subpankou: righttabcontent.xzlxList[righttabindex].xzlxCode
            }));
          } else {
            Toast.fail(data.msg);
          }
  
        }));
      }
      else if (gameCode == 'HK6' && PLitemcode == 'HX') {
      
        let minlen = 0;
        if (righttabindex==0) {
          minlen = 2;
        } else if (righttabindex==1) {
          minlen = 4;
        } else if (righttabindex==2) {
          minlen = 6;
        }
      
        if (Object.keys(select).length < minlen) {
          Toast.fail(`请至少选择${minlen}个生肖`);
          return ;
        }
  
        let nums = [];
        for (let peiyuItemId in select) {
          nums.push(peiyuItemId);
        }
  
        dispatch(getMultiGroupOrder(gameCode, nums, minlen, (ok,data) => {
  
          if (ok) {
            dispatch(tempSaveSelectedPeiyu(select));
            dispatch(tempSaveSelectedNameAndMoney(name,money))
            history.push('/ordermoney?'+  buildQuery({
              gameCode: gameCode,
              pankou: PLitemcode,
              subpankou: righttabcontent.xzlxList[righttabindex].xzlxCode
            }));
          } else {
            Toast.fail(data.msg);
          }
  
        }));
      }
      else if (gameCode == 'HK6' && PLitemcode == 'WSL') {
       
        let minlen = 0;
        if (righttabindex==0) {
          minlen = 2;
        } else if (righttabindex==1) {
          minlen = 3;
        } else if (righttabindex==2) {
          minlen = 4;
        }
      
        if (Object.keys(select).length < minlen) {
          Toast.fail(`请至少选择${minlen}个尾数`);
          return ;
        }
  
        let nums = [];
        for (let peiyuItemId in select) {
          nums.push(peiyuItemId);
        }
  
        dispatch(getMultiGroupOrder(gameCode, nums, minlen, (ok,data) => {
  
          if (ok) {
            dispatch(tempSaveSelectedPeiyu(select));
            dispatch(tempSaveSelectedNameAndMoney(name,money))
            history.push('/ordermoney?'+  buildQuery({
              gameCode: gameCode,
              pankou: PLitemcode,
              subpankou: righttabcontent.xzlxList[righttabindex].xzlxCode
            }));
          } else {
            Toast.fail(data.msg);
          }
  
        }));
      }else if(gameCode == 'GDKLSF' && PLitemcode == 'LM'){
        let minlen = 0;
        if (righttabindex==0) {
          minlen = 2;
        } else if (righttabindex == 1) {
          minlen = 3;
        } else if (righttabindex == 2) {
          minlen = 4;
        } else if (righttabindex == 3) {
          minlen = 5;
        }
      
        if (Object.keys(select).length < minlen) {
          Toast.fail(`请至少选择${minlen}个号码`);
          return ;
        }
  
        let nums = [];
        for (let peiyuItemId in select) {
          nums.push(peiyuItemId);
        }
  
        dispatch(getMultiGroupOrder(gameCode, nums, minlen, (ok,data) => {
  
          if (ok) {
            dispatch(tempSaveSelectedPeiyu(select));
            dispatch(tempSaveSelectedNameAndMoney(name,money))
            history.push('/ordermoney?'+  buildQuery({
              gameCode: gameCode,
              pankou: PLitemcode,
              subpankou: ["RX2","RX3","RX4","RX5"][righttabindex]
            }));
          } else {
            Toast.fail(data.msg);
          }
  
        }));
      }
      else if (  ['CQSSC', 'XJSSC'].indexOf(gameCode) != -1 &&  ['EZDW', 'SZDW', 'EZZH'].indexOf(PLitemcode) != -1  ) {
        if (Object.keys(select).length <= 0) {
          Toast.fail('请选择号码');
          return ;
        }
  
        let item = select[Object.keys(select)[0]];
        
        let numbers = unserializeContent(item.number);
        // console.log(numbers,"mmdmdmdm");        
        let minlen = 2;
        if (PLitemcode == 'SZDW') {
          minlen = 3;
        }
        for (var I in numbers) {
          var R = numbers[I];
          R.length <= 0 && delete numbers[I]
        }
        let isOk = true;
        if (Object.keys(numbers).length !== minlen ) {
          isOk = false;
        } else {
          for (let key in numbers) {
            let nums = numbers[key];
            if (nums.length <= 0 ) {
              isOk = false;
            }
          }
        }
  
        if (!isOk) {
          Toast.fail(`请选择${minlen}组号码`);
          return ;
        }
        select[Object.keys(select)[0]].number = serializeContent(numbers)
        // console.log(select,"mmdmdmdm1");   
        dispatch(tempSaveSelectedPeiyu(select));
        dispatch(tempSaveSelectedNameAndMoney(name,money))
        history.push('/ordermoney?'+  buildQuery({
          gameCode: gameCode,
          pankou: PLitemcode,
          subpankou: "QSW"
        }));
      }
      else if (  ['CQSSC', 'XJSSC'].indexOf(gameCode) != -1 &&  ['ZXL', 'ZXS'].indexOf(PLitemcode) != -1  ) {
        let minlen = 5, multilen = 3;
        if (PLitemcode == 'ZXL') {
          minlen = 4;
          multilen = 6;
        }
        
        if (Object.keys(select).length < minlen) {
          Toast.fail(`请至少选择${minlen}个号码`);
          return ;
        }
  
        let nums = [];
        for (let peiyuItemId in select) {
          nums.push(select[peiyuItemId].number);
        }
        let xzlxList = righttabcontent.xzlxList[righttabindex].xzlxCode
        dispatch(getGroupOrder(gameCode, PLitemcode, xzlxList, multilen, nums, (ok,data) => {
          if (ok) {
            dispatch(tempSaveSelectedPeiyu(select));
            dispatch(tempSaveSelectedNameAndMoney(name,money))
            history.push('/ordermoney?'+  buildQuery({
              gameCode: gameCode,
              pankou: PLitemcode,
              subpankou: xzlxList
            }));
          } else {
            Toast.fail(data.msg);
          }
  
        }));
      }
      else {
        if (Object.keys(select).length <= 0) {
          Toast.fail('请选择号码');
          return ;
        }else if(gameCode == 'HK6' && PLitemcode == 'GG' && Object.keys(select).length <= 1){
          Toast.fail('请至少选择2个号码');
          return ;
        }
        dispatch(tempSaveSelectedPeiyu(select));
        dispatch(tempSaveSelectedNameAndMoney(name,money))
        history.push('/ordermoney?'+buildQuery({
          gameCode: gameCode,
          pankou:PLitemcode,
        }))
      }
    }
  
   render() {
     const {Index,betcenter,match,award,dispatch} = this.props
     const gameCode = match.params.key
     const betcenteritem = betcenter.get("pankoulist")[gameCode]
     const peilvitem = betcenter.get("peilv")[gameCode]
     const gameinfo=betcenter.get("gameinfo")
     if(!betcenteritem || !peilvitem) {
      //  console.log(betcenteritem,peilvitem,"de");
       return <div className="loading-box"><div className="loading-start">
       </div><p className="msg">数据加载中···</p></div>
     }
     const title = betcenteritem.gameName || ""
     let itemList = betcenteritem.itemList 
     if(!itemList && gameCode =="BJKL8"){
       itemList= [{itemName: "特码", itemCode: "TM"},{itemName: "特码包三", itemCode: "TMSB"}]
     }
     const defaultcontent =  itemList[0] 
     const menu = {
        alertTit:"类型选择",
        alertitems: []
      }
    Index.get("playlist").map((item,index)=>{
      menu.alertitems.push({
          title:item.gameName,
          key: item.gameCode
      })
    })
    //  console.log(this.state.itemcode,"123");
    const {righttabindex,itemcode,select,defaultradio} = this.state
    let PLitemcode =  itemcode || betcenter.get("defaultitemcode")[gameCode]
    let body = gameCode =="BJKL8" ? peilvitem["TM"] :peilvitem[PLitemcode]
  
    let righttabcontent = this.state.righttabcontent || defaultcontent
    const {fenpanSecond,kaijiangSecond} = this.state
    //  console.log(defaultradio,"fu");
     
   return <div className={"caipiao "+ (this.state.fenpanZhong && "fenpan " )+ (this.state.kaijiangZhong && "kaijiangzhong")}>
     <Header title={title} {...this.props} 
     tabs={menu} LeftClick={"/"} defaultradio={defaultradio} tabsChoose={this.tabsChoose}></Header>
 
     <BetCenterHistory  gameCode= {gameCode} history={betcenter.get("servenAward")[gameCode]} currentQs={gameinfo.currentQs}
       fpSecond={ fenpanSecond}
       kjSecond={ kaijiangSecond }
       kaijiang={this.onKaijiangFinish.bind(this)}
       fenpan={this.onFenpanFinish.bind(this)}
       fenpanZhong = {this.state.fenpanZhong}
       loadCaiPiaoData={()=>  {gameCode=="BJKL8" ? dispatch(getGameInfo(gameCode,"TM")) : dispatch(getGameInfo(gameCode,PLitemcode)) , dispatch(loadServenawardDetail(gameCode,1)) }}
      ></BetCenterHistory>

      <div className="bet ">
    <div className="bet_page">
      <div className="left_bet_page">
        <BetCenterLeftTab {...this.props} 
         gameCode= {gameCode} PLitemcode={PLitemcode}
        itemList={itemList} lefttab={this.leftTab}></BetCenterLeftTab>
      </div>
      <div className="bet_choice_number">
        <BetCenterRight 
          {...this.props}
          itemcode= {PLitemcode}
          gameCode= {gameCode}
          righttabcontent ={righttabcontent}
          righttabindex = {righttabindex}
          body={body}
          select={select}
          righttab={this.righttab}
          order={this.order.bind(this)} // 箭头函数没有arguments
        ></BetCenterRight>

      </div>
    
    </div>
    <div className="xizhu">
      <div className="total">
        <div onClick={this.reset.bind(this)}><span><i className="iconfont icon-lajitong"></i></span></div>
        <div><span>{this.state.num}</span>注</div>
        <div>共<span>{this.state.totalmoney}</span>元</div>
      </div>
      <div className="money">
        <input type="num" placeholder="投注金额" value={this.state.money}
          onChange={(e)=>{this.inputmoney(e.target.value)}}
        />
      </div>
      <div className="autoclick" onClick={this.autoclick.bind(this,body,righttabcontent,PLitemcode,righttabindex)}>
          机选一注
      </div>
      <div className="submit" onClick={this.onBetSubmit.bind(this)}>
        确认
      </div>
    </div>
    </div>
   </div>
   }
}

function mapStateToProps(state) {
  return {
    Index: state.Index,
    betcenter: state.betcenter,
    award: state.award
  }
}

export default connect(mapStateToProps)(BetCenterContainer)